/*
 Copyright 2007 Razvan Taranu

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package generators;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Utils {
    /**
     * Reads content from the file, returns a string.
     * 
     * @param dirfile
     * @return
     * @throws IOException
     */
    public static String readFile(String dirfile) {
        StringBuffer stringBuffer = new StringBuffer(1024);

        char[] buf = new char[1024];
        int numRead = 0;

        try {
            BufferedReader reader = new BufferedReader(new FileReader(dirfile));
            while ((numRead = reader.read(buf)) != -1) {
                stringBuffer.append(buf, 0, numRead);
            }
            reader.close();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        return stringBuffer.toString();
    }

    public static String readRessource(String name) {
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        InputStream input = cl.getResourceAsStream(name);
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[1024];
        int numRead = 0;

        try {
            while ((numRead = reader.read(buffer)) != -1) {
                builder.append(buffer, 0, numRead);
            }
            reader.close();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        return builder.toString();
    }

    /**
     * Writes content to the file, replacing an existing file.
     * 
     * @param content
     * @param outputFile
     */
    public static void writeFile(String content, File outputFile) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
            writer.write(content);
            writer.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Writes content to the file, replacing an existing file.
     * 
     * @param content
     * @param outputFile
     * @throws IOException
     */
    public static void writeFile(String content, String outputFile) {
        writeFile(content, new File(outputFile));
    }

    /**
     * Writes content to the file, and renames an existing file by adding the suffix.
     * 
     * @param content
     * @param dirfile
     * @param suffix
     * @throws IOException
     */
    public static void writeFile(String content, String dirfile, String suffix) throws IOException {
        File fileOut = new File(dirfile);

        if (fileOut.exists()) {
            fileOut.renameTo(new File(dirfile + suffix));
        }

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileOut));
        writer.write(content);
        writer.close();
    }
}
